import type { Recordable } from '@vben/types';

import { requestClient } from '#/api/request';

export namespace SystemDeptApi {
  export interface Dept {
    [key: string]: any;
    id: string;
    code: string;
    name: string;
    desc?: string;
    status: 0 | 1;
    order: number;
    parent_id: number;
    created_at: string;
    updated_at: string;
    children?: Dept[];
  }
}

/**
 * 获取部门树形列表
 */
async function getDeptList(params?: Recordable<any>) {
  return requestClient.get<Array<SystemDeptApi.Dept>>('/v1/dept/list', {
    params,
  });
}

/**
 * 根据ID获取部门详情
 */
async function getDeptById(id: number | string) {
  return requestClient.get<SystemDeptApi.Dept>(`/v1/dept/${id}`);
}

/**
 * 创建部门
 */
async function createDept(
  data: Omit<
    SystemDeptApi.Dept,
    'children' | 'created_at' | 'id' | 'updated_at'
  >,
) {
  return requestClient.post('/v1/dept/', data);
}

/**
 * 更新部门
 */
async function updateDept(data: SystemDeptApi.Dept) {
  return requestClient.put('/v1/dept/', data);
}

/**
 * 删除部门
 */
async function deleteDept(id: number | string) {
  return requestClient.delete(`/v1/dept/${id}`);
}

export { createDept, deleteDept, getDeptById, getDeptList, updateDept };
